{--
    This package provides compatible definitions for Haskell 2010's
    System.Environment but mostly they do not translate to a Java
    environment.

    getEnv can be made to work once it is wired into how main is
    executed (it's currently a stub and will be updated after Ingo
    has completed a rewrite of main / runMain).

    getProgName really has no equivalent in the Java world since
    it is based on the UNIX idea of an executable name or the
    symbolic link to an executable. For now it arbitrarily returns
    an empty string (returning "java" or "java.exe" would be just
    as (in)accurate).

    getEnv is specified to fail with System.IO.Error.isDoesNotExistError
    if no such environment variable exists, but Frege uses Java
    exceptions so instead we throw an IllegalArgumentException.
    Using the underlying (Java) System.getenv is recommended
    since that returns Maybe String instead
--}
module frege.system.Environment where

import frege.java.Lang

private native argv frege.run.RunTM.argv :: JArray String

getArgs :: IO [String]
getArgs = pure (toList argv)

getProgName :: IO String
getProgName = return ""

getEnv :: String -> IO String
getEnv s =
    case System.getenv s of
        -- temporary 'new' name until name lookup bug fixed
        Nothing -> throwIO (IllegalArgumentException.new s)
        Just v  -> return v
